package cn.wolfcode.trip.core.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserinfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicalException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.Userinfo;
import cn.wolfcode.luowowo.member.service.IUserinfoService;
import cn.wolfcode.trip.core.mapper.UserinfoMapper;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.v2.model.runtime.RuntimeMapPropertyInfo;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.web.servlet.server.Session;

import javax.servlet.http.Cookie;
import java.util.List;
import java.util.UUID;


@Service
public class UserinfoServiceImpl implements IUserinfoService {

    @Autowired
    private UserinfoMapper mapper;
    @Reference
    private IUserinfoRedisService redisService;
    @Override
    public Userinfo getById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean checkPhone(String phone) {
        Integer num=mapper.checkNumByPhone(phone);
        if(num>0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void sendVerifyCode(String phone) {
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        StringBuilder sb=new StringBuilder(100);
        sb.append("验证码是：").append(code).append(",").append(Consts.VERIFY_CODE_VAI_TIME*60).append("秒后到期");
        System.out.println(sb.toString());
        redisService.storeCode(phone,code);
    }

    @Override
    public void userRegist(String phone,String nickname, String password, String rpassword,String verifyCode)throws LogicalException {
        AssertUtil.notEmpty(phone,"手机不能为空");
        AssertUtil.notEmpty(nickname,"昵称不能为空");
        AssertUtil.notEmpty(password,"密码不能为空");
        AssertUtil.notEmpty(rpassword,"确认密码不能为空");
        AssertUtil.notEmpty(verifyCode,"验证码不能为空");

        AssertUtil.equivalent(password,rpassword,"密码不一致");

        if(this.checkPhone(phone)) {
            throw new LogicalException("手机号已经被注册");
        }

        String test_phone="verify_code:"+phone;
        String result=redisService.getCode(test_phone);
        if(result==null || !result.equalsIgnoreCase(verifyCode)) {
            throw new LogicalException("验证码不正确或已失效");
        }

        Userinfo userinfo=new Userinfo();
        userinfo.setNickname(nickname);
        userinfo.setPassword(password);
        userinfo.setPhone(phone);
        userinfo.setState(0);
        userinfo.setHeadImgUrl("images/default.jpg");
        mapper.insert(userinfo);
    }

    @Override
    public Userinfo loginIn(String phone, String password) throws LogicalException {
        AssertUtil.notEmpty(phone,"手机号不能为空");
        AssertUtil.notEmpty(password,"密码不能为空");

        Userinfo userinfo=mapper.loginIn(phone,password);
        if(userinfo==null) {
            throw new LogicalException("用户名或密码不正确");
        }
        return userinfo;
    }

    @Override
    public List<Userinfo> list() {
        return mapper.selectAll();
    }
}
